<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { Toast } from 'vant'
import dayJs from 'dayjs'
import { PatchUserProfile } from '@/api/user.js'
export default {
  data () {
    return {
      currentDate: new Date(this.value || '2000-01-01'),
      minDate: new Date('1900-01-01'),
      maxDate: new Date()
    }
  },

  created () {

  },
  props: {
    value: {
      type: String
    }
  },
  methods: {
    // 点击确认
    async onConfirm (val) {
      Toast.loading({
        duration: 0,
        message: '加载中...',
        forbidClick: true
      })
      const formatTime = dayJs(val).format('YYYY-MM-DD')
      try {
        await PatchUserProfile({
          birthday: formatTime
        })
        Toast.success('更新成功！')
        this.$emit('input', formatTime)
        this.$emit('close')
      } catch (err) {
        Toast.fail('更新失败！')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
